//
//  MSKitchenProductsController.m
//  觅尚
//
//  Created by No.8 on 2019/5/6.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSWinProducctsController.h"
#import "MSKitchenProductsCell.h"
#import "MSKitchenProductsModel.h"

@interface MSWinProducctsController ()<UITableViewDelegate,UITableViewDataSource>
{
    CustomNavigationBar *naviagtionBar;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *listArray;

/** 数组*/
@property(nonatomic,strong)UILabel *chukulabel;

@end

@implementation MSWinProducctsController
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    self.listArray = [NSMutableArray new];
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    //     [self ReloadData];
}
- (void)MJChiBaoZiHeaderRefresh{
    
    [self ReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    
    [self tableView];
    
    [self CreatView];
    
    
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom] + 220 * px, ScreenWidth, ScreenHeight - [self navBarBottom] - 220 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (void)CreatView{
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, 220 * px)];
    HeaderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:HeaderView];
    
    //    /** time*/
    //    UILabel * timelabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * px, 20 * px, 100 * px, 60 * px)];
    //    timelabel.textColor = [UIColor whiteColor];
    //    timelabel.text = @"日期";
    //    timelabel.textAlignment = NSTextAlignmentLeft;
    //    timelabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    [HeaderView addSubview:timelabel];
    //
    //
    //    UITextField * TimeFeild = [[UITextField alloc] init];
    //    TimeFeild.frame =  CGRectMake(CGRectGetMaxX(timelabel.frame) + 10 * px, timelabel.y - 10 * px, 200 * px, timelabel.height + 20 * px);
    //    TimeFeild.textColor = [UIColor whiteColor];
    //    TimeFeild.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    TimeFeild.backgroundColor = BackViewColor;
    //    [HeaderView addSubview:TimeFeild];
    //
    //    /** 物品名称*/
    //    UILabel * Namelabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * px, CGRectGetMaxY(timelabel.frame) + 40 * px, 200 * px, timelabel.height)];
    //    Namelabel.textColor = [UIColor whiteColor];
    //    Namelabel.text = @"物品名称";
    //    Namelabel.textAlignment = NSTextAlignmentLeft;
    //    Namelabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    [HeaderView addSubview:Namelabel];
    //
    //
    //
    //    UITextField * NameFeild = [[UITextField alloc] init];
    //    NameFeild.frame =  CGRectMake(CGRectGetMaxX(Namelabel.frame) + 10 * px, Namelabel.y - 10 * px, 200 * px, TimeFeild.height);
    //    NameFeild.textColor = [UIColor whiteColor];
    //    NameFeild.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    NameFeild.backgroundColor = BackViewColor;
    //    [HeaderView addSubview:NameFeild];
    //
    //
    //
    //    /** 物品名称*/
    //    UILabel * RoomNblabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(NameFeild.frame) + 30 * px, Namelabel.y, 150 * px, timelabel.height)];
    //    RoomNblabel.textColor = [UIColor whiteColor];
    //    RoomNblabel.text = @"房间号";
    //    RoomNblabel.textAlignment = NSTextAlignmentLeft;
    //    RoomNblabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    [HeaderView addSubview:RoomNblabel];
    //
    //
    //
    //    UITextField * RoomNbFeild = [[UITextField alloc] init];
    //    RoomNbFeild.frame =  CGRectMake(CGRectGetMaxX(RoomNblabel.frame) + 30 * px, Namelabel.y, 150 * px, timelabel.height);
    //    RoomNbFeild.textColor = [UIColor whiteColor];
    //    RoomNbFeild.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    RoomNbFeild.backgroundColor = BackViewColor;
    //    [HeaderView addSubview:RoomNbFeild];
    //
    //
    //    /** 搜索按钮*/
    //    UIButton * seacheButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(RoomNbFeild.frame) + 30 * px, Namelabel.y, 150 * px, Namelabel.height)];
    //    seacheButton.backgroundColor = FontColor;
    //    [seacheButton setTitle:@"搜索" forState:UIControlStateNormal];
    //    seacheButton.titleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    //    [seacheButton  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    ////    [seacheButton  addTarget:self action:@selector(<#name#>) forControlEvents:UIControlEventTouchUpInside];
    //    [HeaderView addSubview:seacheButton];
    
    
    
    
    /** 物品名称*/
    UILabel * chukulabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * px,20 * px, ScreenWidth / 2, 60 * px)];
    chukulabel.textColor = [UIColor whiteColor];
    chukulabel.text = @"出库:3000件";
    chukulabel.textAlignment = NSTextAlignmentLeft;
    chukulabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [HeaderView addSubview:chukulabel];
    self.chukulabel = chukulabel;
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"YYYY.MM.dd"];
    NSDate *datenow = [NSDate date];
    
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    /** 物品名称*/
    UILabel * timelabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, chukulabel.y, ScreenWidth / 2 - 50 * px, 60 * px)];
    timelabel.textColor = [UIColor whiteColor];
    timelabel.text = [NSString stringWithFormat:@"%@",currentTimeString];
    timelabel.textAlignment = NSTextAlignmentRight;
    timelabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
    [HeaderView addSubview:timelabel];
    
    
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(chukulabel.frame) + 30 * px, HeaderView.width, 120 * px)];
    titleView.backgroundColor = BackViewColor;
    [HeaderView addSubview:titleView];
    
    
    NSArray * titleArray = @[@"图片",@"名称/单价",@"房间",@"数量",@"总价"];
    
    CGFloat CommoditytitleWidth = HeaderView.width / titleArray.count;
    CGFloat CommoditytitleY = 0;
    CGFloat CommoditytitleHeight = titleView.height;
    for (int index = 0; index < titleArray.count; index ++) {
        /** 标题label*/
        UILabel * Commoditytitle = [[UILabel alloc] init];
        Commoditytitle.x = CommoditytitleWidth * index;
        Commoditytitle.y = CommoditytitleY;
        Commoditytitle.width = CommoditytitleWidth;
        Commoditytitle.height = CommoditytitleHeight;
        Commoditytitle.textColor = WYCRGBColor(102, 121, 145);
        Commoditytitle.text = titleArray[index];
        Commoditytitle.textAlignment = NSTextAlignmentCenter;
        Commoditytitle.font = [UIFont systemFontOfSize:SYRealValue(30 / 2)];
        [titleView addSubview:Commoditytitle];
    }
    
}
- (void)ReloadData{
    
    [MBManager showLoading];
    [self.listArray removeAllObjects];
    NSDictionary * params = @{@"belongBusid":[WYCAccountTool unarchivebelongBusid],@"orderType":@1};
    [HTTPRequest POST:MSgetPreciousSale params:params succeed:^(id responseObject) {
        WYCLog(@"-----------%@",responseObject);
        if (CodeStr) {
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            self.listArray = [MSKitchenProductsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"preciousRoomList"]];
            WYCLog(@"--------%lu",(unsigned long)self.listArray.count);
            //            if (responseObject[@"data"][@"preciousCount"] == nil || responseObject[@"data"][@"preciousCount"] == NULL || [responseObject[@"data"][@"preciousCount"] isKindOfClass:[NSNull class]] || [[responseObject[@"data"][@"preciousCount"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0){
            //                self.chukulabel.text = @"0";
            //            }else{
            self.chukulabel.text = [NSString stringWithFormat:@"出库:%@件",responseObject[@"data"][@"preciousCount"]];
            //            }
        }else{
            [MBManager hideAlert];
            [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBManager hideAlert];
        [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"厨品数据";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSKitchenProductsCell * cell = [MSKitchenProductsCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.KitchenProductsModel = self.listArray[indexPath.row];
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}


- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
